<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html>
<head>

    <title>Home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="keywords" content=""/>
    <%@include file="../my_js_css/cssInclude.jsp" %>
    <meta charset="UTF-8">
    <script type="application/x-javascript"> addEventListener("load", function () {
        setTimeout(hideURLbar, 0);
    }, false);

    function hideURLbar() {
        window.scrollTo(0, 1);
    } </script>

</head>
<body>
<%@include file="../my_js_css/jsInclude.jsp" %>
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
<script src="http://cache.amap.com/lbs/static/es5.min.js"></script>
<script src="http://webapi.amap.com/maps?v=1.4.4&key=a9cc973368bc35f8eae77052967ebcb1&callback=loadMap&plugin=AMap.PolyEditor,AMap.MouseTool,AMap.ToolBar"></script>
<div class="page-container">
    <!--/content-inner-->
    <div class="left-content">
        <div class="inner-content">
            <!-- header-starts -->
            <div class="header-section">
                <!--menu-right-->
                <div class="top_menu" style="height: 80px; text-align: center;">
                    <span style="display: inline-block; line-height: 80px; font-size: 30px; font-weight: 600; color: #fff">
                         智慧旅游
                    </span>

                    <a style=" float: right; color: #fff; margin: 26px 23px 0 0; font-size: 19px;" class="tooltips" href="/Tourism">
                        <i class="lnr lnr-power-switch"></i>
                    </a>



                    <div class="clearfix"></div>

                </div>
                <!--//menu-right-->
                <div class="clearfix"></div>
            </div>
            <!-- //header-ends -->
            <!--outter-wp        民生服务管理-->
            <div class="outter-wp">
                <div class="sub-heard-part">
                    <ol class="breadcrumb m-b-0">
                        <li><a href="index.html">系统管理</a></li>
                        <li class="active">网格管理</li>
                    </ol>
                </div>

                <div class="page-body">
                    <%--<div class="widget-header">--%>
                        <%--<span class="widget-caption">查询条件</span>--%>
                    <%--</div>--%>
                    <div class="widget-body">
                        <div class="graph">
                            <form action="${ctx}/civilManage/queryGrids" name="form" id="form" method="post">
                                <div class="row">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label for="query_org_id" class="col-sm-4 control-label" style="text-align: right; padding: 0;">网格所属社区</label>
                                            <div class="col-sm-8">
                                                <select id="query_org_id" name="query_org_id" class="form-control1">
                                                    <option value="">--全部--</option>
                                                    <c:forEach items="${org_ids}" var="org">
                                                        <option value="${org.org_id}">${org.org_name}</option>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label for="query_grid_name" class="col-sm-4 control-label" style="text-align: right; padding: 0;">网格名称</label>
                                            <div class="col-sm-8">
                                                <input type="text" class="form-control" id="query_grid_name" name="query_grid_name" maxlength="20" placeholder="网格名称" />
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label for="query_emp_name" class="col-sm-4 control-label" style="text-align: right; padding: 0;">网格长</label>
                                            <div class="col-sm-8">
                                                <input type="text" class="form-control" id="query_emp_name" name="query_emp_name" maxlength="20" placeholder="网格长" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group  text-right">
                                            <button id="btn_query" class="btn btn-primary" type="button" style="font-size: 14px; margin: 0; padding: 8px 20px;">
                                                <span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查询网格信息
                                            </button>
                                            <button id="btn_add" class="btn btn-success" type="button"  style="font-size: 14px; margin: 0; padding: 8px 20px;"
                                                    data-toggle="collapse" data-target="#addForm" aria-expanded="false" aria-controls="addForm">
                                                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 新增网格信息
                                            </button>

                                        </div>

                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="collapse" id="addForm">
                        <div>
                            <%--<div class="widget-header">--%>
                                <%--<span class="widget-caption">网格信息</span>--%>
                                <%--<div class="widget-buttons">--%>
                                    <%--<a href="#" data-toggle="collapse">--%>
                                        <%--<i class="fa fa-minus"></i>--%>
                                    <%--</a>--%>
                                <%--</div>--%>
                            <%--</div>--%>
                            <div class="widget-body" style="height:500px;">
                                <form action="${ctx}/civilManage/saveGrid" name="form1" id="form1" method="post">
                                    <input type="hidden" id="grid_id" name="grid_id" value="">
                                    <input type="hidden" id="gridProportion" name="grid_proportion" value="">

                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="remark">网格标识</label>
                                                <input id="remark" name="remark" type="text" class="form-control"
                                                       maxlength="50"
                                                       placeholder="网格标识">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="grid_name">网格名称</label>
                                                <input id="grid_name" name="grid_name" type="text" class="form-control"
                                                       maxlength="50"
                                                       placeholder="网格名称">
                                            </div>
                                        </div>

                                    </div>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="org_id">网格所属社区</label>
                                                <select id="org_id" name="org_id" class="form-control1">
                                                    <option value="">--全部--</option>
                                                    <c:forEach items="${org_ids}" var="org">
                                                        <option value="${org.org_id}">${org.org_name}</option>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="emp_name">网格长</label>
                                                <span class="input-icon icon-right inverted">
                                                            <input id="emp_name" name="emp_name"
                                                                   type="text" readonly
                                                                   placeholder="网格长" class="form-control">
                                                                <i id="btn_select_grid_owner"
                                                                   class="fa fa-user success circular"></i>
                                      </span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <div id="container" class="form-control" style="height:300px;">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-4 col-md-offset-8">
                                            <div class="form-group text-right">
                                                <button id="btn_submit" class="btn btn-success" type="button" onclick="from1submit()">
                                                    <span class="glyphicon glyphicon-saved" aria-hidden="true"></span>
                                                    保存网格信息
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <!--选取网格长-->
                    <div id="selectOwner" style="display:none;">
                        <table class="table table-striped table-bordered table-hover">
                            <thead>

                            <th>

                            </th>
                            <th>
                                人员名称
                            </th>
                            <th>
                                所属社区
                            </th>

                            </thead>
                            <tbody id="selectGridOwnerTable">

                            </tbody>
                        </table>
                    </div>


                </div>

                <div class="row">

                    <div class="col-xs-12 col-md-12">

                        <div class="radius-bordered bordered-bottom bordered-themeprimary">
                            <div class="widget-header ">
                                <span class="widget-caption">查询结果</span>
                                <div class="widget-buttons">
                                    <a href="#" data-toggle="collapse">
                                        <i class="fa fa-minus"></i>
                                    </a>
                                </div>
                            </div>
<style>
    .table>tbody>tr>td, .table>tbody>tr>th, .table>tfoot>tr>td, .table>tfoot>tr>th, .table>thead>tr>td, .table>thead>tr>th {
        vertical-align: initial;
        text-align: center;
    }
    .table td, .table>tbody>tr>td, .table>tbody>tr>th, .table>tfoot>tr>td, .table>tfoot>tr>th {
        padding: 0px !important;
    }
</style>
                            <div class="widget-body">
                                <table class="table table-striped table-bordered table-hover" id="simpledatatable">
                                    <thead class="bordered-darkorange">
                                    <th>#</th>
                                    <th>网格名称</th>
                                    <th>网格长</th>
                                    <th>所属社区</th>
                                    <th>网格标识</th>
                                    <th><span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> 操作</th>
                                    </thead>
                                    <tbody id="archives_tbody">

                                    </tbody>
                                </table>
                                <%--<div class="form-group" style="padding:10px;">--%>
                                <%--<button id="btn_append" class="btn btn-warning" style="display: none" data-toggle="tooltip" data-placement="top" data-original-title="" type="button">--%>
                                <%--<span class="glyphicon glyphicon-plus" aria-hidden="true">--%>

                                <%--</span> 加载更多...--%>
                                <%--</button>--%>
                                <%--</div>--%>
                                <div class="form-group" style="padding:10px;">
                                    <ul id="jqPaginator" class="pagination">

                                    </ul>


                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!--//outer-wp-->
            <!--footer section start-->

        </div>
    </div>
    <!--//content-inner-->
    <!--/sidebar-menu-->
    <div class="sidebar-menu">
        <header class="logo">
            <a href="#" class="sidebar-icon"> <span class="fa fa-bars"></span> </a>
            <a href="${ctx}/sys/index "> <span id="logo"> <h1>智慧旅游</h1></span> </a>
        </header>
        <div class="down">
            <a   style="display: inline-block;">
                <img style="width: 58px; border: 3px solid #ddd; margin-top: -30px" src="http://202.97.207.34:8199/user.png">
            </a>
            <a    style="display: inline-block; margin-left: 10px; text-align: left;">
                <span style="font-size: 15px; display: inline-block; margin-left: 9px;" class=" name-caret">${emp_name}</span><br />
                <p style="display: inline-block;" id="clock3"></p>
            </a>
        </div>
        <div style="border-top:1px solid rgba(69, 74, 84, 0.7)"></div>

        <div class="menu">
            <ul id="menu">
                <li><a href="${ctx}/work/addEvent"><i class="lnr lnr-pencil"></i> <span>上报事件</span></a></li>
                <li><a href="${ctx}/work/zworkIndex"><i class="fa fa-file-text-o"></i> <span>组任务</span></a></li>
                <li><a href="${ctx}/work/gworkIndex"><i class="fa fa-file-text-o"></i> <span>个人任务</span></a></li>
                <li><a href="${ctx}/work/insertEvent"><i class="lnr lnr-pencil"></i> <span>提交事件流</span></a></li>
                <li><a href="${ctx}/work/queryAllEvent"><i class="lnr lnr-pencil"></i> <span>查询事件</span></a></li>
                <li id="menu-academico">
                    <a><i class="fa fa-table"></i> <span> 系统管理</span> <span class="fa fa-angle-right" style="float: right"></span></a>
                    <ul id="menu-academico-sub">
                        <li><a href="${ctx}/emp/empIndex">人员管理</a></li>
                        <li><a href="${ctx}/org/orgIndex">部门管理</a></li>
                        <li><a href="${ctx}/role/roleIndex">角色管理</a></li>
                        <li><a href="${ctx}/sys/grid">网格管理</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
    <div class="clearfix"></div>
</div>

<script>
    window.onload = function(){
        //要执行的js代码段
        _query(1)
    }
    var page = 1;
    var map;
    var infoWin;
    var editor = {};
    var gridList = [];
    var pathLocation;
    //    $("#btn_append").click(function () {
    //        page = page + 1;
    //        _query(page);
    //    });
    function from1submit() {
        var form1val = $("#form1").formSerialize();
        $.post("${ctx}/civilManage/saveGrid"  ,form1val, function (d) {
            var content = document.getElementsByClassName("amap-marker-content").item(0);
            if (content != null) {
                $("#gridProportion").val(content.textContent);
                console.log(d);
                if (d == -1) {
                    alert("系统错误，请再试或联系系统管理员！");
                    return false;
                } else {
                    if ($.isEmptyObject($("#grid_id").val())) {
                        $(pathLocation).each(function (index, data) {
                            console.log(data["M"], data["O"]);
                            alert(JSON.stringify(data))
                            $.getJSON('${ctx}/civilManage/addGridLocation/' + d, {
                                xlocation: data["O"],
                                ylocation: data["P"],
                                seq: index
                            }, function (result) {

                            });
                        })
                        alert("新增成功！");
                        setTimeout(location.href = "${ctx}/civilManage/gridIndex", 1000);
                    } else {
                        var xpath = "";
                        var ypath = "";
                        if (pathLocation.size() > 0) {
                            $(pathLocation).each(function (index, data) {
                                console.log(data["M"], data["O"]);
                                xpath += data["M"] + ",";
                                ypath += data["O"] + ","
                            })
                            console.log(xpath, ypath);
                            $.getJSON('${ctx}/civilManage/updateGridLocation/' + d, {
                                xlocation: xpath,
                                ylocation: ypath
                            }, function (result) {

                            });
                            alert("修改成功！");
                            setTimeout(location.href = "${ctx}/civilManage/gridIndex", 1000);
                        } else {
                            alert("修改成功！");
                            setTimeout(location.href = "${ctx}/civilManage/gridIndex", 1000);
                        }

                    }
                }
            } else {
                alert("请绘制网格！");
            }
        })
    }
    function addGrid(gridId) {
        var url;
        gridList = [];
        if (gridId != null) {
            url = "${ctx}/emergency/queryAllGridByNot/" + gridId;
        } else {
            url =  "${ctx}/civilManage/queryAllGrid/";
        }
        $.get(url, function (result) {
            var obj = eval(result);
            $(obj).each(function (index, data) {
                console.log(data);
                $.get("${ctx}/civilManage/queryGridLocation/" + data.grid_id, function (result) {
                    if (result != null) {
                        var obj = eval(result);
                        var polygonArr = new Array();//多边形覆盖物节点坐标数组
                        $(obj).each(function (index, data) {
                            var location = [];
                            location[0] = data.gridLocationX;
                            location[1] = data.gridLocationY;
                            location[2] = data.grid_id;
                            polygonArr.push(location);
                        })
                        var polygon = new AMap.Polygon({
                            path: polygonArr,//设置多边形边界路径
                            strokeColor: "#FF33FF", //线颜色
                            strokeOpacity: 0.2, //线透明度
                            strokeWeight: 3,    //线宽
                            fillColor: "#1791fc", //填充色
                            fillOpacity: 0.35,//填充透明度
                            exeData: data.grid_id

                        });
                        polygon.setMap(map);

                        polygon.on('mouseover', function (e) {
                            infoWin = new AMap.InfoWindow({
                                isCustom: true,
                                content: "",
                                offset: new AMap.Pixel(0, -20)
                            });

                            infoWin.setContent(gridWindow(data.grid_name));
                            infoWin.open(map, e.lnglat);
                        });
                        polygon.on('mouseout', function (e) {
                            closeInfoWindow();
                        })

                        gridList.push(polygon);
                    }
                });
            })
        });
    }

    function gridWindow(title) {
        var info = document.createElement("div");
        info.style.position = "relative";
        info.className = "info";
        info.style.width = '100%';
        info.style.border = "2px solid #97cbf4";
        //可以通过下面的方式修改自定义窗体的宽高
        //info.style.width = "400px";

        // 定义顶部标题
        var top = document.createElement("div");
        top.className = "info-top";
        info.appendChild(top);


        // 定义中部内容
        var middle = document.createElement("div");
        middle.className = "info-middle";
        middle.style.width = "100%";
        middle.style.background = "black";
        middle.innerHTML = title;
        info.appendChild(middle);
        console.log(info);
        return info;
    }
    //关闭信息窗体
    function closeInfoWindow() {
        map.clearInfoWindow();
    }

    function loadMap(gridId) {
        map = null;
        //新增
        map = new AMap.Map('container', {
            resizeEnable: true,
            center: [130.975966,45.300046],
            zoom: 13
        });
        addGrid(gridId);
//            map.plugin('AMap.Geolocation', function() {
//                geolocation = new AMap.Geolocation({
//                    enableHighAccuracy: true,//是否使用高精度定位，默认:true
//                    timeout: 10000,          //超过10秒后停止定位，默认：无穷大
//                    buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
//                    zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
//                    buttonPosition:'RB'
//                });
//                map.addControl(geolocation);
//                geolocation.getCurrentPosition();
//            });
        var mouseTool = new AMap.MouseTool(map);   //在地图中添加MouseTool插件
        mouseTool.polygon(); //用鼠标工具画多边形
        AMap.event.addListener(mouseTool, 'draw', function (e) { //添加事件
            pathLocation = null;
            pathLocation = e.obj.getPath();

            $(gridList).each(function (index, polygon) {
                $(pathLocation).each(function (index, path) {
                    if (polygon.contains(new AMap.LngLat(path["lng"], path["lat"]))) {
                        alert("所绘网格包含了其他网格！请重新绘制！");
                        mouseTool.close(true);
                        mouseTool.polygon(); //用鼠标工具画多边形
                        return false;
                    }
                })
            })
        });
        mouseTool.measureArea();
    }
    $("#btn_query").click(function () {
        $("#archives_tbody").empty();
        page = 1;
        _query(page);
    });
    function _query(p) {
        $("#archives_tbody").empty();
        $.getJSON("${ctx}/civilManage/queryGrids/", function (datas) {
            var total = "";
            $.each(datas, function (idx, item) {
                console.log(datas)
                var grid_name = item.grid_name;
                var emp_name = item.emp_name;
                var org_name = item.org_name;
                var remark = item.remark;
                if (grid_name == null) {
                    grid_name = "";
                }
                if (emp_name == null) {
                    emp_name = "";
                }
                if (org_name == null) {
                    org_name = "";
                }
                if (remark == null) {
                    remark = "";
                }
                var trInfo = '<tr>                                                                                                                                                                                                                                    ';
                trInfo += '	<td>' + ((10 * (page - 1)) + (idx + 1)) + '</td>                                                                                                                                                                                                                           ';
                trInfo += '	<td>' + grid_name + '</td>                                                                                                                                                                                                                           ';
                trInfo += '	<td>' + emp_name + '</td>                                                                                                                                                                                                                           ';
                trInfo += '	<td>' + org_name + '</td>                                                                                                                                                                                                                           ';
                trInfo += '	<td>' + remark + '</td>                                                                                                                                                                                                                           ';
                trInfo += '<td>                                                                                                                                                                                                                ';

                trInfo += '<button id=\"btn_edit#' + item.grid_id + '\"  onclick=\"return upd(\'' + item.grid_id + '\')\"  name=\"btn_edit\"  class=\"btn btn-info btn-xs edit\" data-toggle=\"collapse\" ';
                trInfo += 'data-target="#addForm" aria-expanded="false" ';
                trInfo += ' aria-controls=\"addForm\"><i class=\"fa fa-edit\"></i> 修改 ';
                trInfo += ' </button> ';
                trInfo += '<button id=\"btn_del#' + item.grid_id + '\" onclick=\"return del(\'' + item.grid_id + '\')\" class=\"btn btn-danger btn-xs delete\"><i class=\"glyphicon glyphicon-minus\"></i> 删除 </button>';

                trInfo += '	</td>                                                                                                                                                                                                                                  ';
                trInfo += '</tr>                                                                                                                                                                                                                                   ';
                $("#archives_tbody").append(trInfo);
                // $("#btn_append").attr("data-original-title","共:"+item.total+"条\n"+"剩余:"+(item.total-((10*(page-1))+(idx+1)))+"条");
                total = item.total;
            });
            $("#jqPaginator").empty();
            $.loadpage("#jqPaginator", Math.ceil(total / 10), p);

            //按钮权限判断
            checkRole();

        });
    }
    function upd(id) {


        $("#form1").data('bootstrapValidator').resetForm();
        $("#addForm").collapse('show');
        $.getJSON('${ctx}/civilManage/getGridById', {"girdId": id}, function (result) {
            $("#grid_id").val(result.grid_id);
            // alert(result.emp_name);
            $("#grid_name").val(result.grid_name);
            $("#emp_name").val(result.emp_name);
            $("#grid_mem_name").val(result.grid_mem_name);
            $("#org_id").val(result.org_id);
            $("#remark").val(result.remark);
            $("#xyz").val(result.xyz);
            sessionStorage.setItem("selectedGridOwners", result.grid_owner_id);//已选网格长id
            sessionStorage.setItem("selectedGridMems", result.grid_mem_id);//已选网格员id
            loadMap(result.grid_id);
        });
    }
    function del(id) {
        if (confirm("确认删除？")) {
            $.post('${ctx}/civilManage/delGridById/' + id, function (result) {
                alert("删除成功!");
                location.href = "${ctx}/civilManage/gridIndex";
            });
        }
    }
    function init() {
        _query(1);
        validator();
    }
    $(function () {
        init();
    });

    function checkRole() {
        //菜单权限设置
        var userRole = $.getUserRoles();
        var roles = "";
        for (var i = 0; i < userRole.length; i++) {
            roles += userRole[i].roleID + ",";
        }
//            只有role_id=39 (人口网格编辑)才显示修改、删除、新增按钮
        if (roles.indexOf("39") < 0) {
            //隐藏添加按钮
            $("#btn_add").attr("style", "display:none;");
            //隐藏删除按钮
            $("button[id^='btn_del']").each(function () {
                $(this).attr("style", "display:none;");
            })
            //隐藏修改按钮
            $("button[id^='btn_edit']").each(function () {
                $(this).attr("style", "display:none;");
            })
        }
    }

    $("#btn_add").on("click", function () {
        $("#form1").resetForm(true);
        $("#form1").data('bootstrapValidator').resetForm();
        loadMap();
    });

    $("#btn_select_grid_owner").on("click", function () {
        if ($.isEmptyObject($("#org_id").val())) {
            alert("请先选择网格属地");
            return;
        }
        var org_id = $("#org_id option:selected").val();
        $.getJSON('${ctx}/civilManage/getGridOwnerList', {"org_id": org_id}, function (result) {
            var content = "";//将网格长列表加到model窗体中
            $(result).each(function (index, item) {
                content += "<tr>";
                content += "<td>";
                content += "<div  class=\"checkbox\">";
                content += " <label>";
                if (!$.isEmptyObject(sessionStorage.getItem("selectedGridOwners")) && sessionStorage.getItem("selectedGridOwners").indexOf(item.emp_id) >= 0) {
                    content += "      <input name=\"select_grid_owner\" checked=\"checked\" id=\"" + item.emp_id + "\"  type=\"checkbox\" value=\"" + item.emp_id + ":" + item.emp_name + "\" >";
                    content += "       <span class=\"text\">选择</span>";
                } else {
                    content += "      <input name=\"select_grid_owner\"  type=\"checkbox\" value=\"" + item.emp_id + ":" + item.emp_name + "\" >";
                    content += "       <span class=\"text\">选择</span>";
                }
                content += "</label>";
                content += "</div>";
                content += "</td>";
                content += "<td>" + item.emp_name + "</td>";
                content += "<td>" + item.org_name + "</td>";
                content += "</tr>";
            });
            $("#selectGridOwnerTable").empty();
            $("#selectGridOwnerTable").append(content);
            bootbox.dialog({
                message: $("#selectOwner").html(),
                title: "选择网格长",
                className: "modal-darkorange",
                buttons: {
                    success: {
                        label: "选择",
                        className: "btn-blue",
                        callback: function () {
                            $("#selectGridOwnerTable").empty();
                            var owners = "";
                            $("[name=\"select_grid_owner\"]:checkbox").each(function () {
                                if ($(this).is(":checked")) {
                                    owners += ($(this).val() + ",");
                                }
                            });
                            $("#emp_name").val(owners.substring(0, owners.length - 1));

                        }
                    }
                }
            });
            sessionStorage.removeItem("selectedGridOwners");
        });
    });


    function validator() {
        $("#form1").bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            submitHandler: function (form) {
                console.log(form);
                var content = document.getElementsByClassName("amap-marker-content").item(0);
                if (content != null) {
                    $("#gridProportion").val(content.textContent);
                    $("#form1").ajaxSubmit({
                        success: function (d) {
                            console.log(d);
                            if (d == -1) {
                                alert("系统错误，请再试或联系系统管理员！");
                                return false;
                            } else {
                                if ($.isEmptyObject($("#grid_id").val())) {
                                    $(pathLocation).each(function (index, data) {
                                        console.log(data["M"], data["O"]);
                                        $.getJSON('${ctx}/civilManage/addGridLocation/' + d, {
                                            xlocation: data["M"],
                                            ylocation: data["O"],
                                            seq: index
                                        }, function (result) {

                                        });
                                    })
                                    alert("新增成功！");
                                    setTimeout(location.href = "${ctx}/civilManage/gridIndex", 1000);
                                } else {
                                    var xpath = "";
                                    var ypath = "";
                                    if (pathLocation.size() > 0) {
                                        $(pathLocation).each(function (index, data) {
                                            alert(JOSN.stringify(data));
                                            console.log(data["M"], data["O"]);
                                            xpath += data["M"] + ",";
                                            ypath += data["O"] + ","
                                        })
                                        console.log(xpath, ypath);
                                        $.getJSON('${ctx}/civilManage/updateGridLocation/' + d, {
                                            xlocation: xpath,
                                            ylocation: ypath
                                        }, function (result) {

                                        });
                                        alert("修改成功！");
                                        setTimeout(location.href = "${ctx}/civilManage/gridIndex", 1000);
                                    } else {
                                        alert("修改成功！");
                                        setTimeout(location.href = "${ctx}/civilManage/gridIndex", 1000);
                                    }

                                }
                            }
                        }
                    });
                } else {
                    alert("请绘制网格！");
                }
            },
            fields: {
                grid_name: {
                    validators: {
                        notEmpty: {
                            message: '网格名不能为空'
                        },
                        stringLength: {
                            min: 2,
                            max: 50,
                            message: '网格名最小2个中文最长50个中文'
                        },
                        remote: {
                            message: '网格名已经存在！',
                            data: function (validator) {
                                return {
                                    grid_id: $("#grid_id").val()
                                };
                            },
                            url: '${ctx}/civilManage/checkGridName'
                        }
                    }
                },
                emp_name: {
                    validators: {
                        notEmpty: {
                            message: '网格长不能为空'
                        }
                    }
                },
                org_id: {
                    validators: {
                        notEmpty: {
                            message: '网格所属社区不能为空'
                        }
                    }
                },
                remark: {
                    validators: {
                        notEmpty: {
                            message: '网格标识不能为空'
                        }
                    }
                }

            }
        });

    }
</script>
</body>
</html>